package top.yunmouren.areamusic.event;

import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import top.yunmouren.areamusic.AreaMusic;
import top.yunmouren.areamusic.config.AreaDto;
import top.yunmouren.areamusic.exception.PlayerNullException;
import top.yunmouren.areamusic.player.PlayerBridge;
import top.yunmouren.areamusic.utils.MusicUtil;

import java.util.List;

public class PlayerJoinListener extends MusicUtil implements Listener {
    private final AreaMusic plugin;

    public PlayerJoinListener(AreaMusic plugin) {
        super(plugin);
        this.plugin = plugin;
    }

    @EventHandler
    public void onEvent(PlayerJoinEvent event) {
        try {
            PlayerBridge playerBridge = new PlayerBridge(event.getPlayer(), this.plugin);
            playerBridge.clearPlayerMusicTag();
            if (playerBridge.getPlayerMusicTag() == null) {
                List<AreaDto> insideList = this.getInsideList(playerBridge.getPlayer().getWorld().getName(), playerBridge.getPlayer().getLocation().getX(), playerBridge.getPlayer().getLocation().getY(), playerBridge.getPlayer().getLocation().getZ());
                if (insideList.size() == 0) {
                    return;
                }

                AreaDto max = this.selectMostImportantArea(insideList);
                this.play(playerBridge, max);
            }
        } catch (PlayerNullException ignored) {
        }

    }
}